package com.example.permission.controller;

import com.example.permission.common.page.Page;
import com.example.permission.common.result.Result;
import com.example.permission.entity.dto.SysMenuDTO;
import com.example.permission.entity.dto.page.SysMenuPageDTO;
import com.example.permission.entity.po.SysMenu;
import com.example.permission.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 权限功能
 *
 * @author xuwq
 * @date 2020-05-09 15:56:07
 */
@RestController
@Api(tags = "权限功能")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("v1/menu")
    @ApiOperation("新增")
    public Result<SysMenu> add(@RequestBody @Valid SysMenuDTO sysMenuDTO) {
        return Result.success(this.sysMenuService.insert(sysMenuDTO));
    }

    @PutMapping("v1/menu/{menuId}")
    @ApiOperation("修改")
    public Result<SysMenu> edit(@PathVariable("menuId") Integer menuId, @RequestBody @Valid SysMenuDTO sysMenuDTO) {
        sysMenuDTO.setMenuId(menuId);
        return Result.success(this.sysMenuService.update(sysMenuDTO));
    }

    @DeleteMapping("v1/menu/{menuId}")
    @ApiOperation("删除")
    public Result<Boolean> deleteById(@PathVariable("menuId") Integer menuId) {
        return Result.success(this.sysMenuService.deleteById(menuId));
    }

    @GetMapping("v1/menu/{menuId}")
    @ApiOperation(value = "根据id查询数据")
    public Result<SysMenu> queryById(@PathVariable("menuId") Integer menuId) {
        return Result.success(this.sysMenuService.queryById(menuId));
    }

    @PostMapping("v1/menu/multi")
    @ApiOperation(value = "查询菜单列表")
    public Result<Page<SysMenu>> queryListByPage(@RequestBody SysMenuPageDTO sysMenuPageDTO) {
        return Result.success(this.sysMenuService.queryListByPage(sysMenuPageDTO));
    }
}